﻿/*****************************************************************************
	SNEGA STG GAME ENGINE 0.7a
	Copyright (C) 2009, Xiaohan Tian (snega0223)
	
	http://code.google.com/p/sng-actionscript-stg-engine/
	
	This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License(LGPL) as
	published by the Free Software Foundation, either version 3 of the 
	License, or(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU Lesser General Public License for more details.
	
	You should have received a copy of the GNU Lesser General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*****************************************************************************/

package com.sng.stg.game.enemy 
{
	import com.sng.stg.game.enemy.action.EnemyAction;
	import com.sng.stg.game.event.ContinuousEvent;
	import com.sng.stg.game.event.EnemyKilled;
	import com.sng.stg.game.StageViewer;
	import flash.display.*;
	
	/**
	 * ...
	 * @author snega
	 */
	public class Enemy extends Sprite
	{
		public var _currStage:StageViewer = null;
		
		protected var _currBitmap:Bitmap = null;
		
		public var _actionList:EnemyAction = new EnemyAction(false, 0, null);
		public var _currAction:EnemyAction = null;
		
		public var _previous:Enemy = null;
		public var _next:Enemy = null;
		
		public var _offsetPosX:int = 0;
		public var _offsetPosY:int = 0;
		public var _offsetRotX:int = 0;
		public var _offsetRotY:int = 0;
		
		public var _currContinuousEvent:ContinuousEvent = null;
		
		public var _killedAnimation:EnemyKilled = null;
		
		public var _life:int = 1;
		
		public var _width:int = 1;
		public var _height:int = 1;
		
		public function Enemy(bitmapData:BitmapData = null, pixelSnapping:String = "auto", smoothing:Boolean = false) 
		{
			this._currBitmap = new Bitmap(bitmapData, pixelSnapping, smoothing);
			this._currBitmap.x = - this._currBitmap.width/2;
			this._currBitmap.y = - this._currBitmap.height/2;

			this.addChild(this._currBitmap);
			
			this._width = this._currBitmap.width / 2;
			this._height = this._currBitmap.height / 2;
		}
		
		public function push(o:Enemy):void {
			if (this._next == null) {
				o._previous = this;
				
				this._next = o;
			}
			else {
				this._next._previous = o;
				
				o._next = this._next;
				o._previous = this;
				
				this._next = o;
			}
		}
		
		public function remove():void {
			if (this._next != null) {
				this._next._previous = this._previous;
			}
			
			if (this._previous != null) {
				this._previous._next = this._next;
			}
		}
		
		public function start(currStage:StageViewer):void {
			this._currStage = currStage;
		}
		
		public function process():void {
			
		}
		
		public function kill():void {
			if (this._currContinuousEvent != null) {
				this._currContinuousEvent.remove();
			}
			
			this._killedAnimation.x = this.x;
			this._killedAnimation.y = this.y;
			
			this._killedAnimation.start(this._currStage);
			this._currStage._2dElement.removeChild(this);
			this.remove();
		}
		
	}
	
}